package com.Ieasy.Tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/*downimage dw=new downimage();
dw.saveToFile("http://10.81.36.193:8081/png.png");*/
public class DownImage {
    public static void main(String[] args) throws Exception {
        PicFromPrintUtils PicFromPrintUtils = new PicFromPrintUtils();
        PicFromPrintUtils.picToBitmap("http://10.81.36.193:8081/png.png");

       /* NetPrinter NetPrinter = new NetPrinter();
        boolean isopen = NetPrinter.Open("192.168.0.200", 9100);
        NetPrinter.printImage("192.168.0.200");
        NetPrinter.CutPage(5);
        NetPrinter.Close();*/

        //System.out.println(bitmap);
    }
    public static String saveToFile(String args) {

        String url = args;
        byte[] btImg = getImageFromNetByUrl(url);
        if(null != btImg && btImg.length > 0){
            System.out.println("读取到：" + btImg.length + " 字节");
            String fileName = "tclpic.jpg";
            return writeImageToDisk(btImg, fileName);
        }else{
            System.out.println("没有从该连接获得内容");
        }
        return url;
    }


    public static byte[] draw2PxPoint(Bitmap bit) {
        byte[] data = new byte[16290];
        int k = 0;
        for (int j = 0; j < 15; j++) {
            data[k++] = 0x1B;
            data[k++] = 0x2A;
            data[k++] = 33; // m=33时，选择24点双密度打印，分辨率达到200DPI。
            data[k++] = 0x68;
            data[k++] = 0x01;
            for (int i = 0; i < 360; i++) {
                for (int m = 0; m < 3; m++) {
                    for (int n = 0; n < 8; n++) {
                        byte b = px2Byte(i, j * 24 + m * 8 + n, bit);
                        data[k] += data[k] + b;
                    }
                    k++;
                }
            }
            data[k++] = 10;
        }
        return data;
    }
    public int changePointPx1(byte[] arry){
        int v = 0;
        for (int j = 0; j <arry.length; j++) {
            if( arry[j] == 1) {
                v = v | 1 << j;
            }
        }
        return v;
    }

    public static byte px2Byte(int x, int y, Bitmap bit) {
        byte b;
        int pixel = bit.getPixel(x, y);
        int red = (pixel & 0x00ff0000) >> 16; // 取高两位
        int green = (pixel & 0x0000ff00) >> 8; // 取中两位
        int blue = pixel & 0x000000ff; // 取低两位

        int gray = (red*30+green*59+blue*11)/100;
        if ( gray < 128 ){
            b = 1;
        } else {
            b = 0;
        }
        return b;
    }

    /**
     * 将图片写入到磁盘
     * @param img 图片数据流
     * @param fileName 文件保存时的名称
     */
    public static String writeImageToDisk(byte[] img, String fileName){
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File storageDirectory = Environment.getExternalStorageDirectory();
                String absolutePath = storageDirectory.getAbsolutePath();
                System.out.println(absolutePath);
                String filePath = absolutePath+"/"+fileName;
                File file=new File(filePath);//new 一个文件 构造参数是字符串
                File rootFile=file.getParentFile();//得到父文件夹
    //不存在 则创建一个文件
                if(!file.exists()){
                    try {
                        System.out.println(rootFile);
                        rootFile.mkdirs();//！！！！！！！！！！！！！！！！！注意 此处是重点
                        file.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        //e.printStackTrace();
                        System.out.println(file+"文件创建失败");
                    }
                }
                //File file = new File("/tclPic/" + fileName);
                FileOutputStream fops = new FileOutputStream(file);
                fops.write(img);
                fops.flush();
                fops.close();
                System.out.println("图片已经写入到C盘");
                return filePath;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
    /**
     * 根据地址获得数据的字节流
     * @param strUrl 网络连接地址
     * @return
     */
    public static byte[] getImageFromNetByUrl(String strUrl){
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
            byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
            return btImg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 从输入流中获取数据
     * @param inStream 输入流
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}